<?php
/**
 * | @author 韩威兵^life <1877361302@qq.com>
 * +----------------------------------------------------------------------
 * | @copyright 2016 - 2019 HQ
 * +----------------------------------------------------------------------
 * | @version $Id: 2019/4/25 17:38 jiahe_new order.php 韩威兵^life $
 * +----------------------------------------------------------------------
 */

namespace app\common\common\model\order;

use app\common\common\model\NewBase;

class  order extends NewBase  {

    protected $table = 'order';
    //错误消息
    public $error = '';
    //状态值
    public $status = true;
    //其他附带信息
    public $data = [];


    /**
     * 保存订单数据
     * @param $order 订单主表数据
     * @param $detail 订单详情表数据
     * @return int
     */
    public function insertOrderByDetail($order, $details){
        $order_id = $this->save($order);
        if(empty($order_id)){
            $this->error = '保存订单主表出现问题';
            $this->status = false;
            return false;
        }
        $detail['order_id'] = $order_id;
        $detail_model = new orderDetail();
        foreach ($details as $k=>$v){
            $detail_id = $detail_model->insertDetail($v);
            if(empty($detail_id)){
                $this->error = '订单详情数据保存失败，失败的数据为: '.json_encode($v, JSON_UNESCAPED_UNICODE);
                $this->status = false;
                return false;
            }
            $this->data['detail'][] = $detail_id;
        }
        return $order_id;
    }


    /**
     * 不带分页的多条查询
     * @param $where
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function allByWhere($where){
        return $this->where($where)->selectToArray();
    }

    /**
     * 带分页的查询
     * @param $where
     * @param $limit
     * @param string $order
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function selectByWhere($where, $limit, $order='id desc'){
        $list = $this->where($where)->limit($limit)->order($order)->selectToArray();
        $count = $this->where($where)->count();
        $this->data['count'] = $count;
        return $list;
    }

    /**
     * 查询单条数据
     * @param $where
     * @return array|false|\PDOStatement|string|\think\Model
     */
    public function findByWhere($where){
        return $this->where($where)->find();
    }



}  